<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .router-link-active{
            background: red;
            color:#fff;
        }
    </style>
</head>
<body>
    <div id="app">
        <h1>开始学习路由啦</h1>
        <router-link to="/home" tag="button">去home页</router-link>
        <router-link to="/news" tag="button">去新闻页</router-link>
        <router-link to="/mine" tag="button">我的</router-link>
        <hr>
        <router-view></router-view>
    </div>
    <script src="./vue.js"></script>
    <script src="./vue-router.js"></script>
    <script>
        let Home = {
            template:`
                <h1> home组件 </h1>
            `
        }
        let News = {
            template:`
                <h1> news组件 </h1>
            `
        }
        let Mine = {
            template:`
                <div>
                    <h1> 我的组件 </h1>
                    <button @click="logout">退出登录</button>
                </div>
            `,
            methods:{
                logout(){
                    localStorage.removeItem('token');
                    this.$router.go(0);
                }
            }
        }
        let Login = {
            template:`
                <button @click="login">登录</button>
            `,
            methods:{
                login(){
                    // 已经登录成功
                    // 'dwj793r45rfj9r3r'
                    localStorage.setItem("token","4739diesjfeide");
                    this.$router.push("/home")
                }
            } 
        }
        const routes = [
            {
                path:"/",
                redirect:"/home"
            },
            {
                path:"/home",
                component:Home,
                meta:{
                    needLogin:false
                }
            },
            {
                path:"/news",
                component:News,
                meta:{
                    needLogin:false
                }
            },
            {
                path:"/mine",
                component:Mine,
                meta:{
                    needLogin:true
                }
            },
            {
                path:"/login",
                component:Login,
                meta:{
                  
                }
            }
        ];
        const router = new VueRouter({
            routes
        });
        router.beforeEach((to,from,next)=>{
          /* 
          1,先判断是不是去登录或者注册页，是的放行
          2，判断 目标路由是否需要登录
            如果需要
                则 判断 是否登录（token），已经登录 next 否则 去登录
            不需要 
                放行
           */
          if(to.fullPath === "/login" || to.fullPath==="/register"){
              next()
          }else{
            //   是不是需要登录权限
            if(to.meta.needLogin){
                let token = localStorage.getItem("token");
                if(token){
                    next();
                }else{
                    next("/login")
                }
            }else{
                next()
            }
          }
        })
        let vm = new Vue({
            el:"#app",
            router
        })
        /* 
         hash 模式
         根据 url hash值得变化 来 匹配不同的组件
         window.onhashchange
         */
    </script>
</body>
</html>